package com.example.actionsignin.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.actionsignin.R;
import com.example.actionsignin.adapter.RepeatAdapter;
import com.example.actionsignin.mvp.presenter.IReleaseListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 旭 on 2017/8/5/005.
 */

public class RepeatCustomFragment extends DialogFragment implements
        View.OnClickListener,
        AdapterView.OnItemClickListener{

    private View view;
    private ListView mListView;
    private Button btn_no;
    private Button btn_ok;

    private RepeatAdapter mAdapter;
    private List<Map<String,Object>> mList = new ArrayList<>();

    private String[] mDate = {"周一","周二","周三","周四","周五","周六","周日"};

    private IReleaseListener listener = null;

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout((int) (dm.widthPixels * 0.9f),getDialog().getWindow().getAttributes().height);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        Window window = this.getDialog().getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.windowAnimations = R.style.BottomDialogAnimation;
        window.setAttributes(layoutParams);

        view = inflater.inflate(R.layout.fragment_dialog_repeat_custom,container,false);
        init();

        return view;
    }

    private void init()
    {
        mListView = (ListView) view.findViewById(R.id.list_repeat_custom);
        btn_ok = (Button) view.findViewById(R.id.ok_repeat_custom);
        btn_no = (Button) view.findViewById(R.id.no_repeat_custom);

        setData();
        listener = (IReleaseListener) getActivity();

        mAdapter = new RepeatAdapter(getActivity().getApplicationContext(),mList);
        mListView.setAdapter(mAdapter);

        mListView.setItemsCanFocus(true);
        mListView.setOnItemClickListener(this);

        btn_ok.setOnClickListener(this);
        btn_no.setOnClickListener(this);
    }

    private void setData()
    {
        for (int i=0;i<7;++i)
        {
            Map<String,Object> map = new HashMap<>();

            map.put("type",1);
            map.put("date",mDate[i]);
            map.put("choose",false);

            mList.add(map);
        }
    }

    private String getDate()
    {
        String date = "";

        int number = 0;
        for (int i=0; i<mList.size();++i)
        {
            if ((boolean)mList.get(i).get("choose"))
            {
                ++number;
                date += (String) mList.get(i).get("date") + ",";
            }
        }
        if (number == 7)
        {
            date = "每天";
        }

        return date;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mList.get(position).put("choose",!(boolean)mList.get(position).get("choose"));

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id)
        {
            case R.id.ok_repeat_custom:
                String date = getDate();
                listener.sendData(0,date);
                dismiss();
                break;
            case R.id.no_repeat_custom:
                dismiss();
                break;
        }
    }
}
